package cn.rqfund.etl.task;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 调度管理
 * 
 * @author  dengrq dengrq@staff.tianya.cn
 * @date 2013-7-24
 */
public class TaskActivator implements BundleActivator{
	
	private static Logger logger = LoggerFactory.getLogger(TaskActivator.class);
	
	private static String fileName = "quartz.properties";

	Scheduler sched;
	@Override
	public void start(BundleContext context) throws Exception {
		
		Properties properties = getParam();
        
		SchedulerFactory sf = new StdSchedulerFactory(properties);
		sched = sf.getScheduler();
		sched.start();		
	}

	@Override
	public void stop(BundleContext context) throws Exception {
		try {
			if (sched != null) {
				sched.shutdown();
			}
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
		} finally {
			sched = null;
		}		
	}
	
	/**
	 * 初始化参数
	 * 
	 * @throws IOException
	 */
	private Properties getParam() {
		Properties prop = new Properties();
		try {
			InputStream in = ClassLoader.getSystemClassLoader()
					.getResourceAsStream(fileName);
			prop.load(in);
		} catch (Exception ex) {
			logger.error("读取配置文件出错.", ex.getMessage());
		}
		return prop;
	}

}
